package com.example.myshortcutdemo.loading;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import androidx.annotation.IntDef;
import androidx.annotation.Nullable;


import com.example.myshortcutdemo.R;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * @author tgw
 * @date 2022/5/9
 * @describe 加载数据过程中显示加载style的view
 */
public class LoadingProgressView extends IndicatorLoadingView {

    private int mMaxWidth;
    private int mMaxHeight;

    public LoadingProgressView(Context context) {
        super(context);
        init(context, null, 0, 0);
    }

    public LoadingProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0, 0);
    }

    public LoadingProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr, 0);
    }

    private void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LoadingProgressView, defStyleAttr, defStyleRes);
        //默认为100dp*100dp
        mMaxWidth = a.getDimensionPixelSize(R.styleable.LoadingProgressView_maxWidth, context.getResources().getDimensionPixelSize(R.dimen.dimen_100dp));
        mMaxHeight = a.getDimensionPixelSize(R.styleable.LoadingProgressView_maxHeight, context.getResources().getDimensionPixelSize(R.dimen.dimen_100dp));
        setLoadStyle();
        a.recycle();
    }

    public void setLoadStyle() {
        setIndicator(new BallCircleIndicator());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (widthSize > mMaxWidth)
            widthSize = mMaxWidth;
        if (heightSize > mMaxHeight)
            heightSize = mMaxHeight;
        setMeasuredDimension(widthSize, heightSize);
    }

    @IntDef({LoadStyle.ROUND_RECT, LoadStyle.BALL_BEZIER, LoadStyle.BALL_CIRCLE_MOVE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface LoadStyle {
        int ROUND_RECT = 0;
        int BALL_BEZIER = 1;
        int BALL_CIRCLE_MOVE = 2;
    }
}
